package com.github.rpc4j.producer.service;

import java.lang.reflect.Method;
import java.util.concurrent.ConcurrentHashMap;

import net.sf.cglib.reflect.FastClass;
import net.sf.cglib.reflect.FastMethod;

import com.github.rpc4j.producer.core.LifeCycle;
import com.github.rpc4j.producer.util.Utils;
import com.github.rpc4j.util.ServerUtil;

public  class Service implements LifeCycle{
	
    private String serviceInterface;
	
	private Object target;
	
	private String version;
	
	private String serviceName;
	
	private String serviceDesc;
	
	private boolean asynCall;
	
	private int clientTimeout;
	
	private int clientIdleTimeout;
	
	private String serializeType;
	
    private transient FastClass fastClass;
    
    private transient ConcurrentHashMap<String, FastMethod> methodMap = null;

	public String getServiceInterface() {
		return serviceInterface;
	}

	public void setServiceInterface(String serviceInterface) {
		this.serviceInterface = serviceInterface;
	}

	public Object getTarget() {
		return target;
	}

	public void setTarget(Object target) {
		this.target = target;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getServiceName() {
		return serviceName;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	public String getServiceDesc() {
		return serviceDesc;
	}

	public void setServiceDesc(String serviceDesc) {
		this.serviceDesc = serviceDesc;
	}

	public boolean isAsynCall() {
		return asynCall;
	}

	public void setAsynCall(boolean asynCall) {
		this.asynCall = asynCall;
	}

	public int getClientTimeout() {
		return clientTimeout;
	}

	public void setClientTimeout(int clientTimeout) {
		this.clientTimeout = clientTimeout;
	}

	public int getClientIdleTimeout() {
		return clientIdleTimeout;
	}

	public void setClientIdleTimeout(int clientIdleTimeout) {
		this.clientIdleTimeout = clientIdleTimeout;
	}

	public String getSerializeType() {
		return serializeType;
	}

	public void setSerializeType(String serializeType) {
		this.serializeType = serializeType;
	}
	
	public String getServiceKey(){
		StringBuilder sb = new StringBuilder();
		sb.append(ServerUtil.getServerIp()).append("_").append(version);
		return sb.toString();
	}
	
	public ConcurrentHashMap<String, FastMethod> getMethodMap() {
		return methodMap;
	}

	@Override
	public void init() {
		methodMap = new ConcurrentHashMap<String, FastMethod>();
		fastClass = FastClass.create(target.getClass());
		Class interfaceClass = null;
		try {
			interfaceClass =  Class.forName(serviceInterface);
		} catch (ClassNotFoundException e) {
		   throw new RuntimeException(e);
		}
		Method[] methods = interfaceClass.getMethods();
		for(Method method:methods){
			FastMethod fmethod = fastClass.getMethod(method.getName(), method.getParameterTypes());
			String key = Utils.getMethodKey(method);
			methodMap.put(key, fmethod);
		}
	}
	
	@Override
	public void destory() {
		// TODO Auto-generated method stub
		
	}
	
	
}
